﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<div class="modal-dialog x-modal-dialog" style="width: 660px; margin-top: 100px;">

  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">
        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
      </button>
      <h4 class="modal-title">Session&nbsp;-&nbsp;Edit</h4>
    </div>
    
    <form class="form-horizontal ms-form" role="form" method="POST" action="${ctx.host}/session-settings/${session.id}" data-validate="true">
    
      <input type="hidden" name="_method" value="PATCH">
      <input type="hidden" name="sessionId" value="${session.id}">
      
      <div class="modal-body">
        
        <div class="form-group ihg-form-group">
          <label class="col-xs-2 control-label" style="width:20%;">Name</label>
          <div class="col-xs-8">
            <input type="text" class="form-control" name="name" value="${session.name}" data-validate="required"/>
          </div>
        </div>
        
        <div class="form-group ihg-form-group">
          <label class="col-xs-2 control-label" style="width:20%;">Description</label>
          <div class="col-xs-8">
            <textarea class="form-control" name="description" rows="5" data-validate="required">${session.description}</textarea>
          </div>
        </div>
        
        <div class="form-group ihg-form-group">
          <label class="col-xs-2 control-label" style="width:20%;">Time</label>
          <div class="col-xs-8">
            <div class="input-group">
              <input type="text" class="form-control date start-date" name="startDate" data-validate="required" placeholder="start time" value="<fmt:formatDate value="${session.startDate}" pattern="yyyy-MM-dd"/>" readOnly/> <span class="input-group-addon">~</span> <input type="text" class="form-control date end-date" name="endDate" data-validate="required" placeholder="end time" value="<fmt:formatDate value="${session.endDate}" pattern="yyyy-MM-dd"/>" readOnly/>
            </div>
          </div>
        </div>
        
        <div class="form-group ihg-form-group">
          <label class="col-xs-2 control-label" style="width:20%;">Administrator</label>
          <div class="col-xs-4">
            <select name="assessorOwner" class="select2 form-control" data-validate="required">
              <option value=""></option>
              <c:forEach var="admin" items="${persist.administrators}">
                <option value="${admin.id}" ${admin.id == session.assessorOwner.id ? 'selected' : ''}>${admin.name}</option>
              </c:forEach>
            </select>
          </div>
          <div class="col-xs-4">
            <select name="delegateOwner" class="select2 form-control" data-validate="required">
              <option value=""></option>
              <c:forEach var="admin" items="${persist.administrators}">
                <option value="${admin.id}" ${admin.id == session.delegateOwner.id ? 'selected' : ''}>${admin.name}</option>
              </c:forEach>
            </select>
          </div>
        </div>
      </div>
        
      <div class="modal-footer">
        <button type="submit" class="btn btn-success btn-shadow btn-shadow-success x-btn">Submit</button>
        <button type="button" class="btn btn-default btn-shadow btn-shadow-default x-btn" data-dismiss="modal">Cancel</button>
      </div>
      
    </form>
    
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function() {
    
    $(document).find('form[data-validate="true"]').bt_validate() //bind validate
    var $form = $(document).find("form") //bind shiftform
    $form.shiftform({
      beforeSubmit : function(el) {
        $form.trigger("validate.ajax.submit");
        return $.bt_validate.result[$.bt_validate.form_id]
      }
    })
    
    $('.date').datetimepicker({
      format: 'yyyy-mm-dd',
      autoclose: true,
      language: 'zh-CN',
      todayBtn: 'linked',
      startDate: '${today}',
      todayHighlight: true,
      pickerPosition: 'bottom-right',
      minView: 'month'
    });
    
    $('select[name="delegateOwner"]').select2({
      theme: "classic",
      placeholder : 'Delegate Owner',
      minimumResultsForSearch : -1,
      allowClear : true,
    });
    
    $('select[name="assessorOwner"]').select2({
      theme: "classic",
      placeholder : 'Assessor Owner',
      minimumResultsForSearch : -1,
      allowClear : true,
    });
    
    $('.start-date').on('changeDate', function(e){
      var beginDate = new Date($("input[name='startDate']").val());
      var ms = 1 * (1000 * 60 * 60 *24);
      var endDate = new Date(beginDate.getTime()+ms);
      var endDateString = endDate.yyyymmdd("-");
      $('.end-date').val(endDateString);
    });
    
    $('.end-date').on('changeDate', function(e){
      var endDate = new Date($("input[name='endDate']").val());
      var ms = 1 * (1000 * 60 * 60 *24);
      var beginDate = new Date(endDate.getTime()-ms);
      var beginDateString = beginDate.yyyymmdd("-");
      $('.start-date').val(beginDateString);
    });
    
  })
</script>